<template>
  <div class="detail">
    <el-card>
      <el-tabs class="tabs">
        <el-tab-pane label="登录账户设置" lazy>
          <el-form
            ref="form"
            :model="form"
            :rules="rules"
            label-width="100px"
            class="form"
          >
            <el-form-item prop="username" label="姓名">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item prop="password" label="密码">
              <el-input v-model="form.password" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" lazy>
          <component
            :is="'info'"
            :user-info="initUserInfo"
            @setUserName="setUserName"
          />
        </el-tab-pane>
        <el-tab-pane label="岗位详情" lazy>
          <component
            :is="'job'"
          /></el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { sysUserPut } from '@/api/employees'
import { sysUser } from '@/api/user'
export default {
  components: {
    info: () => import('@/views/employees/components/info.vue'),
    job: () => import('@/views/employees/components/job.vue')
  },
  data() {
    return {
      userinfo: {},
      initUserInfo: {},
      id: this.$route.params.id,
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        password: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 6,
            max: 15,
            message: '请输入6-15位密码',
            trigger: 'change'
          }
        ]
      }
    }
  },
  created() {
    this.getData()
  },

  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPut({ ...this.userinfo, ...this.form })
          this.initUserInfo.username = this.form.username
          this.$message.success('更新成功!')
        }
      })
    },
    async getData() {
      const res = await sysUser(this.id)
      this.userinfo = res.data
      this.initUserInfo = JSON.parse(JSON.stringify(res.data))
      this.form.username = res.data.username
      console.log(res)
    },
    setUserName(str) {
      this.form.username = str
    }
  }
}
</script>

<style lang="scss" scoped>
.detail {
  padding: 30px;
  .form {
    width: 600px;
    margin: 0 auto;
    margin-top: 20px;
  }
}
</style>
>
